<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BgcmsProject extends BgcmsProcess {
	var $db;
 	var $smarty;	
	public function __construct($db, $smarty) {
		$this->db = $db;
		$this->smarty = $smarty;
	}
		
	public function __destruct() {
		//
	}
	
	public function show_project_list(){
		$projects = array();
		$countproject = BgcmsProjectBean::count_all_project($this->db);
		$projects['data'] = BgcmsProjectBean::get_all_project_paging($this->db,ADMINPAGING,"list");
		$result = BgcmsPaging::do_paging($countproject,$projects, ADMINPAGING, "?module=BgcmsProject&sub1=ProjectList","list");
		
		$this->smarty->assign("PROJECTS", $result);
		$this->smarty->display('BgcmsProject/index.tpl');
	}
	
  public function process(){
		$sub1 = "ProjectList";
		if(isset($_GET['sub1'])) {
			$sub1 = $_GET['sub1'];
		}
		if($sub1 == "") $sub1 = "ProjectList";
		
		if($sub1 == "ProjectList" || $sub1 == "editproject") {
			$this->process_project($sub1);
		} elseif ($sub1 == "MemberList") {
			$this->process_project_memberlist($sub1);
		}
		$this->show_project_list();
	}
	
	public function process_project($sub1) {
		if(isset($_GET['mess'])) {
			$msg = "";
			switch($_GET['mess']) {
				case 1:
					$msg = _("Project updated.");
					break;
				case 2:
					$msg = _("Project not updated. Maybe you have not change anything.");
					break;
				case 3:
					$msg = _("Project has been added");
					break;		
			}	
			$this->smarty->assign("resp_message", $msg);
		}
		if(isset($_POST['act'])) {
			$act = $_POST['act'];
			if($act == "addproject") {
				$this->create_project();
			} elseif ($act == "editproject") {
				$this->edit_project();
			} elseif($act == "delproject") {
				$this->delete_project();
			}
		} else {
			if($sub1 == "editproject") {
				$id = @$_GET['id'];
				$project = BgcmsProjectBean::get_project_by_id($this->db, $id);
				$this->smarty->assign("THEPROJECT", $project);
			}
		}
		$project_priority = BgcmsProjectBean::get_all_project_priority($this->db);
		$this->smarty->assign("PROJECT_PRIORITY", $project_priority);
	}
	
	public function create_project() {
		$msg = BgcmsProject::validate_project($this->db);
		$arr = array("code" => -1, "message" => _("Could not add Project."));
		if($msg == "") {
			$id = BgcmsProjectBean::save_project($this->db, $_POST);
			if($id >=0 ) {
				$arr = array("code" => 0, "message" => _("New Project has been added."), "project_id" => $id);
				$arr['linkactive'] = "?module=BgcmsProject&sub1=ProjectList&mess=3";
			}
		} else {
			$arr = array("code" => -1, "message" => $msg);
		}
		echo array2json($arr);
				
	}
	
	public function edit_project() {
		if(isset( $_POST['project_id'])) {
			$msgId = 2;
			$msg = $this->validate_project($this->db);
			if($msg == "") {
				$res = BgcmsProjectBean::update_project($this->db, $_POST);
				$msgId = 1;
				$arr = array('ID' => 0,'msg' => 1);
				$arr['linkactive'] = "?module=BgcmsProject&sub1=ProjectList&mess=1";	
			} else {
				$arr = array('ID' => -1, 'message' => $msg);
			}
			echo array2json($arr);
		}
	}
	
	public function delete_project() {
		$arr = array('code' => 0, 'message' => "");
		$id = @$_POST['id'];
		$res = BgcmsProjectBean::delete_project_by_id($this->db, $id);
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Project <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete Project <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
	
    public static function validate_project($db) {
      $msg = "";
    	if($_POST['project_name'] == "") {
			 $msg = "Please type the Project Name.";
		  }
		  if($_POST['project_description'] == "") {
			 $msg = "Please type the Project Description.";
		  }
		  
		  return $msg;
    }
}
?>